﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JYH.Data;
using Newtonsoft.Json;
using JYH.Model;
using JYH.DataAccess;
using JYH.Framework;

namespace JYH.Business
{

    /// <summary>
    ///  SysRoleInfo
    /// </summary> 
    public class SysRoleInfoBLL : BusinessBase<SysRoleInfo>
    {

        /// <summary>
        ///  SysRoleInfo
        /// </summary> 
        public SysRoleInfoBLL()
        {
            myDataBase = DB.D();
            this.SqlSelect = @"; Select SysRoleInfo.* From SysRoleInfo";
        }
        /// <summary>
        /// 依据角色编号和名称查询
        /// </summary>
        /// <param name="Info">查询条件对象</param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public List<SysRoleInfo> GetRoleList(SysRoleInfo Info, out string Msg)
        {
            List<SysRoleInfo> list = new List<SysRoleInfo>();
            Msg = string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase=DB.D();
                }
                Sql sql = new Sql();
                sql.Append(@";Select SysRoleInfo.* From SysRoleInfo");
                if (Info != null) 
                {
                    if (!string.IsNullOrEmpty(Info.RoleNO))
                    {
                        sql.Where("SysRoleInfo.RoleNO Like '%" + Info.RoleNO + "%'");

                    }
                    if (!string.IsNullOrEmpty(Info.RoleName))
                    {
                        sql.Where("SysRoleInfo.RoleName Like '%" + Info.RoleName + "%'");

                    }

                }
                sql.OrderBy("SysRoleInfo.RoleNO");
                list=myDataBase.Fetch<SysRoleInfo>(sql);
            }
            catch (Exception ex) 
            {
                Msg = ex.Message;
                
            }
            return list;
        }

        /// <summary>
        /// 角色保存
        /// </summary>
        /// <param name="Info"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public bool SaveRoleInfo(SysRoleInfo Info, out string Msg)
        { 
            Msg= string.Empty;
            try
            {
                SysRoleInfo obj = new SysRoleInfo();
                Sql sql = new Sql();

                if (Info==null)
                {
                    Msg = "NO Data";
                    return false;
                }
                sql.Append(@";Select SysRoleInfo.* From SysRoleInfo");
                if (!string.IsNullOrEmpty(Info.RoleID))
                {
                    sql.Where("SysRoleInfo.RoleID='" + Info.RoleID + "'");
                    obj = myDataBase.SingleOrDefault<SysRoleInfo>(sql);
                    if (obj == null)
                    {
                        Msg = "NO Data";
                        return false;
                    }
                    else
                    {
                        obj.RoleNO = Info.RoleNO;
                        obj.RoleName = Info.RoleName;
                        myDataBase.Execute(BusinessTool.GetUpdateSql(obj));
                    }
                }
                if (string.IsNullOrEmpty(Info.RoleID))
                {
                    sql.Where("SysRoleInfo.RoleNO='" + Info.RoleNO + "'");
                    obj = myDataBase.SingleOrDefault<SysRoleInfo>(sql);
                    if (obj != null)
                    {
                        obj.RoleName = Info.RoleName;
                        myDataBase.Execute(BusinessTool.GetUpdateSql(obj));
                    }
                    else
                    {
                        obj = new SysRoleInfo();
                        obj.RoleNO = Info.RoleNO;
                        obj.RoleName= Info.RoleName;
                        obj.RState = "1";
                        obj.RoleID = Guid.NewGuid().ToString();
                        myDataBase.Execute(BusinessTool.GetInsertSql(obj));
                    }
                }
                
                return true;
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
                return false;
            }
        }


        /// <summary>
        /// 新增角色用户
        /// </summary>
        /// <param name="Info">角色信息</param>
        /// <param name="Users">用户列表</param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public bool SaveRoleUser(SysRoleInfo Info, List<SysUserInfo> Users, out string Msg)
        {
            Msg=string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                StringBuilder sb=new StringBuilder();
                List<SysRoleUser> RoleUsers = new List<SysRoleUser>();
                Sql sql = new Sql();
                sql.Append(@";Select SysRoleUser.* From SysRoleUser");
                sql.Where("SysRoleUser.RoleID='" + Info.RoleID + "'");
                sql.OrderBy("SysRoleUser.UserID");
                RoleUsers=myDataBase.Fetch<SysRoleUser>(sql);
                if (Users != null && Users.Count > 0) 
                {
                    foreach (SysUserInfo User in Users) 
                    {
                        if (string.Equals(User.IsSelect, "checked") || string.Equals(User.IsSelect.ToString(), "True"))
                        {
                            int index = RoleUsers.FindIndex(p => p.UserID == User.UserID);
                            if (index < 0)
                            {
                                SysRoleUser sys = new SysRoleUser();
                                sys.UserID = User.UserID;
                                sys.RoleID = Info.RoleID;
                                sys.RoleUserID = Guid.NewGuid().ToString();
                                sys.CreateTime = BusinessTool.GetDateTime();
                                sys.CreateUser = Info.CreateUser;
                                RoleUsers.Add(sys);
                                sb.Append(BusinessTool.GetInsertSql(sys));
                            }
                        }
                        else
                        {
                            sb.Append("Delete SysRoleUser Where RoleID='" + Info.RoleID + "' And UserID='" + User.UserID + "'");
                        }


                       
                    }
                }
                if (sb.Length > 0)
                {
                    myDataBase.Execute(sb.ToString());
                }
                return true;
            }
            catch(Exception ex)
            { Msg = ex.Message; return false; }
        }

        /// <summary>
        /// 新增角色菜单
        /// </summary>
        /// <param name="Info">角色信息</param>
        /// <param name="Menus">菜单列表</param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public bool SaveRoleMenu(SysRoleInfo Info, List<SysMenuInfo> Menus, out string Msg)
        {
            Msg = string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                StringBuilder sb = new StringBuilder();
                List<SysRoleMenu> RoleMenus = new List<SysRoleMenu>();
                Sql sql = new Sql();
                sql.Append(@";Select SysRoleMenu.* From SysRoleMenu");
                sql.Where("SysRoleMenu.RoleID='" + Info.RoleID + "'");
                sql.OrderBy("SysRoleMenu.MenuID");
                RoleMenus = myDataBase.Fetch<SysRoleMenu>(sql);
                if (Menus != null && Menus.Count > 0)
                {
                    foreach (SysMenuInfo Menu in Menus)
                    {
                        if (string.Equals(Menu.IsSelect.ToString(), "checked")|| string.Equals(Menu.IsSelect.ToString(), "True"))
                        {
                            int index = RoleMenus.FindIndex(p => p.MenuID == Menu.MenuID);
                            if (index <= -1)
                            {
                                SysRoleMenu sys = new SysRoleMenu();
                                sys.MenuID = Menu.MenuID;

                                sys.RoleID = Info.RoleID;
                                sys.RoleMenuID = Guid.NewGuid().ToString();
                                sys.CreateTime = BusinessTool.GetDateTime();
                                sys.CreateUser = Info.CreateUser;
                                RoleMenus.Add(sys);
                                sb.Append(BusinessTool.GetInsertSql(sys));
                            }
                        }
                        else
                        {
                            sb.Append("Delete SysRoleMenu Where RoleID='"+ Info.RoleID + "' And MenuID='" + Menu.MenuID + "'");
                        }
                
                    }
                }
                if (sb.Length > 0)
                {
                    myDataBase.Execute(sb.ToString());
                }
                return true;
            }
            catch (Exception ex)
            { Msg = ex.Message; return false; }
        }

        /// <summary>
        /// 批量删除角色菜单
        /// </summary>
        /// <param name="RoleMenus">角色菜单列表</param>
        /// <param name="Msg">反馈信息</param>
        /// <returns></returns>
        public bool DeleteRoleMenu(List<SysRoleMenu> RoleMenus, out string Msg)
        {
            Msg= string.Empty;
            try
            {

                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                StringBuilder sb = new StringBuilder();
                foreach (SysRoleMenu sysMenu in RoleMenus)
                {
                    sb.Append(BusinessTool.GetDeleteSql(sysMenu));
                }
                if (sb.Length > 0) 
                {
                    myDataBase.Execute(sb.ToString());
                }
                return true;
            }
            catch (Exception ex)
            { Msg = ex.Message; return false; }
        }

        /// <summary>
        /// 批量删除角色用户
        /// </summary>
        /// <param name="RoleUsers">角色用户列表</param>
        /// <param name="Msg">反馈信息</param>
        /// <returns></returns>
        public bool DeleteRoleUser(List<SysRoleUser> RoleUsers, out string Msg)
        {
            Msg = string.Empty;
            try
            {

                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                StringBuilder sb = new StringBuilder();
                foreach (SysRoleUser sysMenu in RoleUsers)
                {
                    sb.Append(BusinessTool.GetDeleteSql(sysMenu));
                }
                if (sb.Length > 0)
                {
                    myDataBase.Execute(sb.ToString());
                }
                return true;
            }
            catch (Exception ex)
            { Msg = ex.Message; return false; }
        }

        /// <summary>
        /// 获取角色用户列表
        /// </summary>
        /// <param name="Role">角色对象</param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public List<SysRoleUser> GetRoleUserList(SysRoleInfo Role, out string Msg)
        {
            List<SysRoleUser> list = new List<SysRoleUser>();
            Msg = string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                Sql sql = new Sql();
                sql.Append(@";Select SysRoleUser.*,
                                SysRoleInfo.RoleNO,
                                SysRoleInfo.RoleName,
                                SysUserInfo.UserNO,
                                SysUserInfo.UserName
                                From SysRoleUser
                                Inner Join SysRoleInfo ON SysRoleUser.RoleID=SysRoleInfo.RoleID
                                Inner JOin SysUserInfo ON SysRoleUser.UserID=SysUserInfo.UserID");
                sql.Where("SysRoleUser.RoleID='"+Role.RoleID+"'");
                sql.OrderBy("SysUserInfo.UserNO");
                list = myDataBase.Fetch<SysRoleUser>(sql);
            }
            catch (Exception ex){ Msg = ex.ToString(); }
            return list;
        }

        /// <summary>
        /// 获取角色菜单列表
        /// </summary>
        /// <param name="Role"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>

        public List<SysRoleMenu> GetRoleMenuList(SysRoleInfo Role, out string Msg)
        {
            List<SysRoleMenu> list = new List<SysRoleMenu>();
            Msg = string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                Sql sql = new Sql();
                sql.Append(@";Select SysRoleMenu.*,
                                SysRoleInfo.RoleNO,
                                SysRoleInfo.RoleName,
                                SysMenuInfo.MenuNO,
                                SysMenuInfo.MenuName,
                                SysMenuInfo.NameSpace,
                                SysMenuInfo.MenuType,
                                ISNULL(UpMenuInfo.MenuNO,SysMenuInfo.MenuNO) UpMenuNO,
                                UpMenuInfo.MenuName UpMenuName
                                From SysRoleMenu
                                Inner Join SysRoleInfo ON SysRoleMenu.RoleID=SysRoleInfo.RoleID
                                Inner JOin SysMenuInfo ON SysRoleMenu.MenuID=SysMenuInfo.MenuID
                                Left JOin SysMenuInfo UpMenuInfo ON UpMenuInfo.MenuID=SysMenuInfo.UpMenuID");
                sql.Where("SysRoleUser.RoleID='" + Role.RoleID + "'");
                sql.OrderBy("UpMenuInfo.MenuNO,SysMenuInfo.MenuNO");
                list = myDataBase.Fetch<SysRoleMenu>(sql);
            }
            catch (Exception ex) { Msg = ex.ToString(); }
            return list;
        }

        /// <summary>
        /// 批量删除角色信息
        /// </summary>
        /// <param name="Roles">角色列表</param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public override bool Delete(List<SysRoleInfo> Roles, out string Msg)
        {
            Msg = string.Empty;
            try
            {

               
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                StringBuilder sb= new StringBuilder();
                foreach (SysRoleInfo role in Roles) 
                {
                    sb.Append("Delete SysRoleUser Where RoleID='" + role.RoleID + "'");
                    sb.Append("Delete SysRoleMenu Where RoleID='" + role.RoleID + "'");
                    sb.Append(BusinessTool.GetDeleteSql(role));
         
                }
                if (sb.Length > 0)
                {
                    myDataBase.Execute(sb.ToString());
                }
                return true;
            }
            catch(Exception ex) { Msg = ex.ToString(); return false; }
        }

        /// <summary>
        /// 依据角色ID查询所有用户
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public List<SysUserInfo> GetUserOfRole(string RoleID, out string Msg)
        {
            Msg = string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                Sql sql = new Sql();
                sql.Append(string.Format(@";Select 
                                            SysUserInfo.*,
                                            CASE 
                                            WHEN SysRoleUser.RoleUserID IS NULL THEN ''
                                            ELSE 'checked'
                                            END AS IsSelect,
                                            SysRoleUser.RoleID
                                            From SysUserInfo
                                            Left JOin SysRoleUser On SysUserInfo.UserID=SysRoleUser.UserID
                                            And SysRoleUser.RoleID='{0}'
                                            ", RoleID));
                List<SysUserInfo> list = myDataBase.Fetch<SysUserInfo>(sql);
                return list;
            }
            catch (Exception ex)
            {
                Msg = ex.ToString();
                return null;
            }

             
        }


        /// <summary>
        /// 依据角色ID查询所有菜单
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public List<SysMenuInfo> GetMenuOfRole(string RoleID, out string Msg)
        {
            Msg = string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                Sql sql = new Sql();
                sql.Append(string.Format(@";SELECT 
                                        SysMenuInfo.*,
                                        ISNULL(UpMenuInfo.MenuNO, SysMenuInfo.MenuNO) UpMenuNO,
                                        CASE 
                                        WHEN SysRoleMenu.RoleMenuID IS NULL THEN ''
                                        ELSE 'checked'
                                        END AS IsSelect,
                                        SysRoleMenu.RoleID
                                        FROM SysMenuInfo
                                        Left JOin SysMenuInfo UpMenuInfo On SysMenuInfo.UpMenuID=UpMenuInfo.MenuID
                                        Left  JOin SysRoleMenu ON SysMenuInfo.MenuID=SysRoleMenu.MenuID
                                        And SysRoleMenu.RoleID='{0}'
                                        ", RoleID));
                sql.OrderBy("SysMenuInfo.MenuType,SysMenuInfo.NameSpace,ISNULL(UpMenuInfo.MenuNO, SysMenuInfo.MenuNO) Desc ,SysMenuInfo.MenuNO");
                List<SysMenuInfo> list = myDataBase.Fetch<SysMenuInfo>(sql);
                return list;
            }
            catch (Exception ex)
            {
                Msg = ex.ToString();
                return null;
            }


        }




    }

}
